/**
 * 文件名：ContentServiceImpl.java
 *
 * 版本信息：
 * 日期：Sep 1, 2009
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：
 *
 */
package com.dp.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;

/**
 * 
 * 项目名称：platform_p2
 * 类名称：ContentServiceImpl
 * 类描述：
 * 创建人：Administrator
 * 创建时间：Sep 1, 2009 4:01:09 PM
 * 修改人：Administrator
 * 修改时间：Sep 1, 2009 4:01:09 PM
 * 修改备注：
 * @version 
 * 
 */
@Service
@Transactional
public class ContentServiceImpl extends BaseImpl {
	private static Logger log = Logger.getLogger(ContentServiceImpl.class);
	
	@SuppressWarnings("unchecked")
	public boolean updateContentStatus(String updatetimerid) {
	
		ArrayList<String> batchSql = new ArrayList<String>();
		String sql = "update news_extends_channel_title set isnew=2 where channleid in " +
		"(select channleid from news_extends_channel where updatetimer='"+updatetimerid+"' and contentstatus=1)";
		batchSql.add(sql);
		sql = "update news_extends_channel_title set contentstatus=1 where channleid in " +
				"(select channleid from news_extends_channel where updatetimer='"+updatetimerid+"')";
		batchSql.add(sql);
		for(String tmp:batchSql)
		{
			log.info("job: "+ updatetimerid + " excute sql: "+tmp);
		}
		try {
			return this.execTransaction(batchSql);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
	}
	
	/**
	 * 根据QuartzJob调度器更新NewsExtendsChannelTitle.Contentstatus
	 */
	public String updateNctContentstatusByJob(String jobName){
		
		
	    String sql="select channleid from news_extends_channel where updatetimer='"+jobName+"' and channleid in (select channleid from news_extends_channel_title where istest=1 and contentstatus=2)";
	    //要更新的channleid集合  contentstatus=2
	    List<DataMap> list=null;
	    try {
			list=this.queryALLResultSet(sql);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		sql="select channleid from news_extends_channel where updatetimer='"+jobName+"' and channleid in (select channleid from news_extends_channel_title where contentstatus=3)";
	    //要更新的channleid集合3  contentstatus=3,更新状态为4
	    List<DataMap> list3=null;
	    try {
	    	list3=this.queryALLResultSet(sql);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		
		//要更新的频道，频道内容下没有更新的内容 isnew=2 为旧的。
		for(int i=0;i<list.size();i++){
	    	int channleid=Integer.parseInt(list.get(i).getMapData("channleid"));
	    	 sql="update news_extends_channel_title  set isnew=2 where istest=1 and contentstatus=1 and " +
    		"channleid ="+channleid ;     		
         try {
    	
    	     log.info("job: "+ jobName + " excute sql: "+sql);
    	     this.execUpdateRicode(sql);
	         } catch (Exception e) {
	          	e.printStackTrace();
	         } 
	    }
		
		  String channelidlist="";
		  Set<String> channelidSet=new HashSet<String>();
    	  for(int i=0;i<list.size();i++){
	    	int channleid=Integer.parseInt(list.get(i).getMapData("channleid"));
	    	
	    	 sql="update news_extends_channel_title  set contentstatus=1,isnew=1 where istest=1 and contentstatus=2 and " +
    		"channleid ="+channleid ;     		
	    	 
	    	 log.info("job: "+ jobName + " excute sql: "+sql);
	    	 try {
	    		 if(this.execUpdateRicode(sql)>0){
	    			 channelidSet.add(list.get(i).getMapData("channleid"));
	   	         }
	    	 }catch (Exception e) {
		          	e.printStackTrace();
		     } 
	    }
    	  
    	  for(int i=0;i<list3.size();i++){
  	    	int channleid=Integer.parseInt(list3.get(i).getMapData("channleid"));
  	    	 //3 下线更新为 4其它状态 
  	    	 sql="update news_extends_channel_title  set contentstatus=4 where contentstatus=3 and " +
  	    		"channleid ="+channleid ;   
  	    	 try {
  	    		 if(this.execUpdateRicode(sql)>0){
  	    			 channelidSet.add(list3.get(i).getMapData("channleid"));
  	   	         }
  	    	 }catch (Exception e) {
  		          	e.printStackTrace();
  		     } 
  	    }
    	  
    	  for(Iterator<String> it=channelidSet.iterator();it.hasNext();){
  		 	if("".equals(channelidlist)){
  		 		channelidlist=""+it.next();
    	   		}else{
    	   			channelidlist+=","+it.next();
    	   		}
  	      }
    	  
	    return channelidlist;
	    
	} 
}
